package com.snail.config;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import org.apache.http.HttpHost;
import org.apache.http.message.BasicHeader;
import org.elasticsearch.client.RestClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author cici
 * @description es的客户端的配置
 * @date 2025/8/20 17:09
 */
@Configuration
public class ElasticsearchConfig {
    @Value("${spring.elasticsearch.uris:https://172.172.2.39:9200}")
    private String elasticsearchUrl;

    @Bean
    public ElasticsearchClient elasticsearchClient() {
        // 创建 RestClient
        RestClient restClient = RestClient
                .builder(HttpHost.create(elasticsearchUrl))
                .build();

        // 创建 Transport
        ElasticsearchTransport transport = new RestClientTransport(
                restClient,
                new JacksonJsonpMapper()
        );

        // 创建 Client
        return new ElasticsearchClient(transport);
    }
}
